package cn.com.digitalnet.ad.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.com.digitalnet.ad.util.Errors;
import cn.com.digitalnet.ad.util.Holder;

/**
 * 简化在程序的各个位置获得request/response/session/application实例
 * @author qidafang
 */
public class HolderFilter implements Filter{

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		Holder.application = filterConfig.getServletContext();
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest hRequest = (HttpServletRequest)request;
		HttpServletResponse hResponse = (HttpServletResponse)response;
		HttpSession session = hRequest.getSession();
		Holder.request.set(hRequest);
		Holder.response.set(hResponse);
		Holder.session.set(session);
		Holder.errors.set(new Errors());
		
		chain.doFilter(request, response);
	}

	@Override
	public void destroy() {
		
	}

}
